﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using System.Threading.Tasks;
using IdentityServer4.Services;

namespace AuthServer.Controllers
{
    [SecurityHeaders]
    [AllowAnonymous]
    public class HomeController : Controller
    {

        private readonly IIdentityServerInteractionService _interaction;
        private readonly IHostingEnvironment _environment;
        private readonly ILogger<HomeController> _logger;

        public HomeController(IHostingEnvironment environment, ILogger<HomeController> logger,IIdentityServerInteractionService interactionService)
        {
            
            _environment = environment;
            _logger = logger;
            _interaction = interactionService;

            _logger.LogInformation("Hello Home.");
        }

        public IActionResult Index()
        {
            if (_environment.IsDevelopment())
            {

                _logger.LogTrace("...IsDevelopment...");
                _logger.LogInformation("...info.IsDevelopment...");
                // only show in development
                return View();
            }

            _logger.LogInformation("Homepage is disabled in production. Returning 404.");
            return NotFound();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        public async Task<IActionResult> Error(string errorId)
        {
            // retrieve error details from identityserver
            var message = await _interaction.GetErrorContextAsync(errorId);
            if (message != null)
            {
                if (!_environment.IsDevelopment())
                {
                    // only show in development
                    message.ErrorDescription = null;
                }
            }

            return View("Error", message);
        }
    }
}
